package com.admin.education.dataTransferObject.votes.Mapper;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.dataTransferObject.categorys.Mapper.CategoryMapper;
import com.admin.education.dataTransferObject.votes.Request.CreateVotesRequest;
import com.admin.education.dataTransferObject.votes.Request.UpdateVotesRequest;
import com.admin.education.dataTransferObject.votes.VotesTransfer;
import com.admin.education.models.*;
import com.admin.education.repositories.CategorysRepository;
import org.mapstruct.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Mapper
public abstract class VotesMapper {

    private QCategorys qCategorys;

    public VotesMapper() {
        qCategorys = QCategorys.categorys;
    }

    @Autowired
    private VotesDetailMapper votesDetailMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategorysRepository categorysRepository;

    @Mappings({
            @Mapping(target = "votesDetails", ignore = true),
            @Mapping(target = "categorys",ignore = true)
    })
    public abstract VotesTransfer mapFrom(Votes votes);

    @Mappings({
            @Mapping(target = "votesDetails", ignore = true)

    })
    public abstract VotesTransfer mapFrom(Votes votes, @MappingTarget VotesTransfer votesTransfer);

    @Mappings({
            @Mapping(target = "votesDetails",ignore = true),
            @Mapping(target = "categorys",ignore = true)
    })
    public abstract Votes mapFrom(CreateVotesRequest createVotesRequest) throws BadRequestException;

    @Mappings({})
    public abstract List<VotesTransfer> mapFrom(List<Votes> votes);
    @Mappings({
            @Mapping(target = "votesDetails",ignore = true),
            @Mapping(target = "categorys",ignore = true)
    })
    public abstract Votes mapFrom(UpdateVotesRequest updateVotesRequest,@MappingTarget Votes votes)  throws BadRequestException;
    @AfterMapping
    public void afterMapping(Votes votes, @MappingTarget VotesTransfer votesTransfer) {
        if (null != votes.getVotesDetails() && votesTransfer.isInclude()) {
            votesTransfer.setVotesDetails(this.votesDetailMapper.mapFrom(votes.getVotesDetails()));
        }
        if(votesTransfer.isInclude()&&null!=votes.getCategorys()){
            votesTransfer.setCategorys(categoryMapper.mapFrom(votes.getCategorys()));
        }
    }

    @AfterMapping
    public void afterMapping(CreateVotesRequest createVotesRequest, @MappingTarget Votes votes) throws BadRequestException {
        votes.setCategorys(new ArrayList<>());

        if (null != createVotesRequest.getCategorys()) {
            for (Long categoryId : createVotesRequest.getCategorys()) {
                Categorys categorys = this.categorysRepository.findById(categoryId);
                if (null == categorys) {
                    throw new BadRequestException("分类不存在");
                }

                votes.getCategorys().add(categorys);
                categorys.getVotes().add(votes);
            }
        }

        if (null != createVotesRequest.getVotesDetails()) {
            votes.setVotesDetails(this.votesDetailMapper.mapFromCreateEntity(createVotesRequest.getVotesDetails()));
            for (VotesDetail votesDetail : votes.getVotesDetails()) {
                votesDetail.setVotes(votes);
            }
        }
    }
    @AfterMapping
    public void afterMapping(UpdateVotesRequest updateVotesRequest,@MappingTarget Votes votes) throws BadRequestException {
        votes.setCategorys(new ArrayList<>());

        if (null != updateVotesRequest.getCategorys()) {
            for (Long categoryId : updateVotesRequest.getCategorys()) {
                Categorys categorys = this.categorysRepository.findById(categoryId);
                if (null == categorys) {
                    throw new BadRequestException("分类不存在");
                }

                votes.getCategorys().add(categorys);
                categorys.getVotes().add(votes);
            }
        }

        if (null != updateVotesRequest.getVotesDetails()) {
            votes.setVotesDetails(this.votesDetailMapper.mapFromUpdateEntity(updateVotesRequest.getVotesDetails()));
            for (VotesDetail votesDetail : votes.getVotesDetails()) {
                votesDetail.setVotes(votes);
            }
        }
    }
}
